<template>
  <div>
    <el-card class="search-card">
      <BasicForm
        ref="formData"
        defaultExpandStatus
        :isScalingBtn="false"
        :formColumns="formItems"
        :dataSource="formData"
        :handleSearchForm="handleQueryForm"
        @getFormData="getFormData"
      />
    </el-card>
    <el-card class="table-card">
      <SkusTable
        ref="skusTable"
        :showActionCol="false"
        showPagination
        :tableHeight="490"
        :dataSource="tableInfo.list"
        :columns="tableInfo.columns"
        :loading="tableInfo.loading"
        :pageSizes="pageSizes"
        :pagination="pagination"
        :total="pagination.total"
        @pageChange="handlePageChange"
        @pageSizeSelect="handlePageSizeChange"
      />
    </el-card>
  </div>
</template>
<script>
import BasicForm from '@/components/BasicForm';
import SkusTable from '@/components/skusTable';
import { initFormItems, TABLE_COLUMNS } from './constants';
import skusPage from '@/mixins/commodity/skusPage';
import {
  querySkuUpdateLogService,
  querySkuUpdateLogFieldsService
} from '@/api/commodity/commodityManage/editLog';
import { setAuthBtnId } from '@/utils/bizUtils';

export default {
  mixins: [skusPage],
  components: {
    BasicForm,
    SkusTable,
  },
  data () {
    return {
      formItems: initFormItems(),
      formData: {
        spuNo: '',
        skuNo: '',
        enumSkuField: null,
        createTime: '',
        startTime: '',
        endTime: '',
      },
      tableInfo: {
        list: [],
        columns: TABLE_COLUMNS,
        loading: false,
      },
    }
  },
  mounted () {
    this.getTableData()
    this.querySkuUpdateLogFields()
  },
  methods: {
    // 初始化查询条件
    initSearchParams () {
      return _.omit({
        ...this.formData,
      }, 'createTime')
    },
    // 获取表格数据 by 查询表单
    async getTableData () {
      setAuthBtnId('查询')
      const data = {
        ...this.paginationParams,
        ...this.initSearchParams()
      }
      const { retData } = await querySkuUpdateLogService(data, state => { this.tableInfo.loading = state });
      if (retData) {
        const { results, totalNum } = retData;
        this.tableInfo.list = results;
        this.setTotalNum(totalNum)
      }
    },
    async querySkuUpdateLogFields () {
      const res = await querySkuUpdateLogFieldsService({})
      if (res?.retData) {
        const fieldList = res.retData.map(item => ({
          label: item.value,
          value: item.key
        }))
        this.formItems = initFormItems(fieldList)
      }
    }
  }
}
</script>
